package com.example.travelcomponentdemo;

import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class SlideLayout extends Activity implements OnTouchListener {
//	ScrollView scroll1;
//	ScrollView scroll2;
	LinearLayout layout, layout_one, layout_two;
	int width;
	int height;
	LayoutParams layoutscroll1;
	LayoutParams layoutscroll2;
	String TAG = "SlideLayout";
	float previousY;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.slide_layout);
		
//		scroll1 = (ScrollView) findViewById(R.id.scrollView1);
//		scroll2 = (ScrollView) findViewById(R.id.scrollView2);
		layout = (LinearLayout) findViewById(R.id.layout);
		layout_one = (LinearLayout) findViewById(R.id.layout_one);
		layout_two = (LinearLayout) findViewById(R.id.layout_two);
		
		Display display = getWindowManager().getDefaultDisplay();
		DisplayMetrics outMetrics = new DisplayMetrics ();
	    display.getMetrics(outMetrics);
	    
		Point size = new Point();
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			display.getSize(size);

			width = size.x;
			height = size.y;
	    } else {
	    	width = display.getWidth();
	    	height = display.getHeight();
	    }
		Log.e(TAG, "width_screen: " + width);
		Log.e(TAG, "height_screen: " + height);
		layout.setOnTouchListener(this);
		layout_one.setLayoutParams(new LinearLayout.LayoutParams(width, height/2));
		Log.e(TAG, "layout.getTop(): " + layout.getTop());
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		float eventY = event.getRawY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.e(TAG, "ACTION_DOWN: " + eventY);
			previousY = eventY;
			break;
		case MotionEvent.ACTION_UP:
			Log.e(TAG, "ACTION_UP: " + eventY);
			break;
		case MotionEvent.ACTION_MOVE:
			Log.e(TAG, "ACTION_MOVE: " + eventY);
			float y = event.getRawY();
			float diff = y - previousY;
			Log.e(TAG, "ACTION_MOVE layout.getTop: " + layout.getTop());
			Log.e(TAG, "ACTION_MOVE diff: " + diff);
			previousY = y;

			if((layout_two.getHeight() - diff) <= 50 && diff > 0)
				return false;
			
			if((layout.getTop() + diff) <= 50 && diff < 0)
				return false;
			
			layout.offsetTopAndBottom((int) (diff));
			layout_one.setLayoutParams(new LinearLayout.LayoutParams(width, layout.getTop()));
			break;
		default:
			break;
		}
		return true;
	}
}
